﻿using Cache;
using DtoFw;
using DtoFw.Base;
using DtoFw.Query;
using DtoFw.Result;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Service;

namespace SceneEvidenceAPI.Controllers
{
    /// <summary>
    /// 用户
    /// </summary>
    [Route("api/user")]
    public class MainUserController : BaseController
    {
        private readonly MainHomeService rep_home;
        private readonly MainUserService rep_mainUser;
        public MainUserController(MainUserService mainUser, MainHomeService home)
        {
            rep_mainUser = mainUser;
            rep_home = home;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost("login")]
        public BaseGenericResult<MainUserLoginInfo> Login([FromBody] MainUserLoginQuery query)
        {
            var uqkey = Util.Tools.GetUQCode();
            if (!Util.Tools.CheckReg(uqkey))
            {
                return new BaseGenericResult<MainUserLoginInfo>() { Success = false, Code = DtoFw.Enum.EnumReturnCode.SysError, Msg = "系统未激活" };
            }
            return rep_home.Login(query);
        }
        /// <summary>
        /// token刷新
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("refreshtoken/{refreshtoken}")]
        public BaseGenericResult<string> RefreshToken(string refreshtoken)
        {
            return rep_home.RefreshToken(refreshtoken);
        }
        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet("get")]
        public BaseGenericResult<MainUserLoginResult> GetLoginUserInfo()
        {
            return rep_home.GetLoginUserInfo(TokenHelper.GetTokenInfo<TokenData>(User)?.UserId);
        }
        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="query">条件</param>
        /// <returns></returns>
        [HttpGet("pager")]
        public BaseGenericResult<Pager<UserinfoResult>> QueryUserList([FromQuery] UserInfoQuery query)
        {
            return rep_mainUser.QueryList(query);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost("post")]
        public BaseResult Post([FromBody] UserInfoAddInfo info)
        {
            return rep_mainUser.Post(info);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPut("put")]
        public BaseResult Put([FromBody] UserInfoEditInfo info)
        {
            return rep_mainUser.Put(info);
        }
        /// <summary>
        /// 获取用户对象
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet("get/{id}")]
        public BaseGenericResult<UserInfoEditInfo> GetUserInfo(long id)
        {
            return rep_mainUser.Get(id);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPut("put/pwd")]
        public BaseResult PutPassWord([FromBody] PutPwdQuery query)
        {
            return rep_mainUser.PutPassWord(query);
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPut("rest/pwd")]
        public BaseResult RestPassWord([FromBody] RestPwdQuery query)
        {
            return rep_mainUser.RestPassWord(query);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpDelete("delete")]
        public BaseResult Delete([FromBody] DeleteDto query)
        {
            return rep_mainUser.Delete(query);
        }
    }
}
